package com.wms.service.OutStore;

import com.wms.common.BusinessException;
import com.wms.common.PageInfo;
import com.wms.common.Utils;
import com.wms.dao.OutStoreMapper;
import com.wms.domain.*;
import com.wms.dao.StorehouseMapper;
import com.wms.dto.OutStoreDetailDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class OutStoreImpl implements OutStoreService{

    private static final Logger log = LoggerFactory.getLogger(OutStoreImpl.class);
    @Autowired
    private OutStoreMapper outStoreMapper;
    @Autowired
    private StorehouseMapper storehouseMapper;

    @Transactional
    @Override
    public Object addDeliver(OutStoreIdxDomain outStoreIdxDomain) {
        log.info("add Deliver"+outStoreIdxDomain.toString());
        outStoreIdxDomain.setDate(Utils.getDate());
        if(outStoreIdxDomain.getId() != null){
            outStoreMapper.deliverUpdate(outStoreIdxDomain);
            outStoreMapper.deleteDeliverDetail(outStoreIdxDomain.getId());
        }else{
            outStoreIdxDomain.setOut_number(Utils.getTableNumber("out_store_idx","out_number"));
            outStoreMapper.addDeliver(outStoreIdxDomain);
        }

        List<OutStoreDetailDomain> list = outStoreIdxDomain.getDetail();
        for (OutStoreDetailDomain detail :list){
            detail.setOut_store_id(outStoreIdxDomain.getId());
            outStoreMapper.addDeliverDetail(detail);
        }
        return null;
    }

    @Override
    public Object getDeliverPage(OutStoreIdxDomain outStoreIdxDomain) {
        log.info(outStoreIdxDomain.toString());
        PageInfo page = new PageInfo();
        List<HashMap<String,Object>> list = outStoreMapper.getDeliverPage(outStoreIdxDomain);
        List<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
        for (HashMap<String,Object> map : list){
            List<HashMap<String, Object>> detail= outStoreMapper.getOutStoreDetail(((Integer) map.get("id")).longValue());
//            List<OutStoreDetailDomain>detail = outStoreMapper.getOutStoreDetail(((Integer) map.get("id")).longValue());
            if(detail != null){
                map.put("children",detail);
                map.put("row_key",map.get("id"));
            }
            result.add(map);
        }
        page.setData(result);
        page.setCount(outStoreIdxDomain.getBaseCount());
        page.setSum(outStoreIdxDomain.getBaseSum());
        return page;
    }

    @Override
    public Object getDeliverDetailPage(OutStoreDetailDto outStoreDetailDto) {
        log.info(outStoreDetailDto.toString());
        PageInfo page = new PageInfo();
        List<HashMap<String,Object>> list = outStoreMapper.getDeliverDetailPage(outStoreDetailDto);
        page.setData(list);
        page.setCount(outStoreDetailDto.getBaseCount());
        page.setSum(outStoreDetailDto.getBaseSum());
        return page;
    }

    @Transactional
    @Override
    public Object updateDetail(OutStoreDetailDomain outStoreDetailDomain) {
        outStoreMapper.updateDetail(outStoreDetailDomain);
        return null;
    }

    @Transactional
    @Override
    public Object addPicking(List<PickingDomain> pickingDomain) {
        Long store_detail_id = pickingDomain.get(0).getStore_detail_id();
        Long out_store_idx_id =outStoreMapper.getOutStoreIdxIdByDetailId(store_detail_id);
        //修改状态
        OutStoreIdxDomain osid = new OutStoreIdxDomain();
        osid.setId(out_store_idx_id);
        osid.setOut_store_finish(2L);
        osid.setDate(Utils.getDate());
        outStoreMapper.deliverUpdate(osid);

        storehouseMapper.deletePicking(store_detail_id);
        for (PickingDomain pd:pickingDomain){
            storehouseMapper.addPicking(pd);
        }
        BigDecimal sumQty = pickingDomain.stream().map(PickingDomain::getQty
        ).reduce(BigDecimal.ZERO, BigDecimal::add);

        OutStoreDetailDomain osdd = new OutStoreDetailDomain();
        osdd.setId(store_detail_id);
        osdd.setPicking_qty(sumQty);
        outStoreMapper.updateDetail(osdd);
        return null;
    }

    @Override
    public Object getPicking(Long g_id,Long store_detail_id) {
        List<HashMap<String,Object>> storehouse = storehouseMapper.getStorehouseByGid(g_id);
        List<PickingDomain> picking = storehouseMapper.getPicking(store_detail_id);
        List<HashMap<String,Object>> result = new ArrayList<>();
        for (HashMap<String,Object> item: storehouse){
            Long store_location_id = Long.valueOf(String.valueOf(item.get("store_location_id"))) ;
            BigDecimal sumQty = picking.stream().map(s -> {
                if(store_location_id.equals(s.getStore_location_id())){
                    return s.getQty();
                }else {
                    return BigDecimal.ZERO;
                }
            }
            ).reduce(BigDecimal.ZERO, BigDecimal::add);
            item.put("qty",sumQty);
            result.add(item);
        }
        return result;
    }

    private boolean checkSportStat(List<HashMap<String, Object>> detail){
        if(detail == null){
            return true;
        }
//        for (OutStoreDetailDomain qsdd :detail){
//            if(qsdd.getOut_store_stat() != 4L){
//                return true;
//            }
//        }
        for (int i = 0; i < detail.size(); i++) {
            HashMap<String, Object> item = detail.get(i);
            Long outStoreStat = ((Integer)item.get("out_store_stat")).longValue();
            if(outStoreStat != 4L){
                return true;
            }
        }
        return false;
    }
    @Override
    public Object getSportPage(OutStoreIdxDomain outStoreIdxDomain) {
        log.info(outStoreIdxDomain.toString());
        PageInfo page = new PageInfo();
        List<HashMap<String,Object>> list = outStoreMapper.getDeliverPage(outStoreIdxDomain);
        List<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
        for (HashMap<String,Object> map : list){
            List<HashMap<String, Object>> detail= outStoreMapper.getOutStoreDetail(((Integer) map.get("id")).longValue());
            if(checkSportStat(detail)){
                continue;
            }
            map.put("detail",detail);
//            map.put("row_key",map.get("id"));
            result.add(map);
        }
        page.setData(result);
        page.setCount(outStoreIdxDomain.getBaseCount());
        page.setSum(outStoreIdxDomain.getBaseSum());
        return page;
    }


    @Transactional
    @Override
    public Object confirmOutStore(Long id) {
        List<OutStoreDetailDomain> detail = outStoreMapper.getOutStoreDetailForDomain(id);
        for (OutStoreDetailDomain qsdd:detail){
//            StorehouseDomain storehouseDomain = storehouseMapper.getStorehouseByG_idAndStoreL_id(qsdd.getG_id(),qsdd.get);

            List<PickingDomain> pickingList =storehouseMapper.getPicking(qsdd.getId());
            if(pickingList.isEmpty()){
                throw new BusinessException(201,"请先拣货");
            }
            for (PickingDomain picking : pickingList){
                StorehouseDomain storehouseDomain = storehouseMapper.getStorehouseByG_idAndStoreL_id(qsdd.getG_id(),picking.getStore_location_id());
                if(storehouseDomain == null || storehouseDomain.getQty() == null){

                    throw new BusinessException(201,"【"+qsdd.getG_id()+"库存不足");
                }
                //大于等于
                if(storehouseDomain.getQty().compareTo(picking.getQty()) > -1){
                    BigDecimal newQty = storehouseDomain.getQty().subtract(picking.getQty());
                    storehouseDomain.setQty(newQty);
//                    StorehouseDomain tempStorehouse = new StorehouseDomain();
//                    tempStorehouse.setQty(picking.getQty());
//                    tempStorehouse.setG_id(storehouseDomain);
                    storehouseMapper.updateStorehouse(storehouseDomain);
                }else{
                    throw new BusinessException(201,"【"+qsdd.getG_id()+"库存不足");
                }
            }
        }
//        if(id == null){
//            throw new BusinessException(202, "请选择商品");
//        }
//        if(Utils.isBigDecimal(detail.getQty()) || detail.getStore_location_id() == null){
//            throw new BusinessException(202,"当前货位数量不可以小于0");
//        }
        return null;



//        BigDecimal result1 = num1.add(num2);  ++
//        BigDecimal result2 = num1.subtract(num2); --
//        BigDecimal result3 = num1.multiply(num2); *
//        BigDecimal result5 = num2.divide(num1,20,BigDecimal.ROUND_HALF_UP); /
//
//        if(a.compareTo(b) == -1){
//            System.out.println("a小于b");
//        }
//
//        if(a.compareTo(b) == 0){
//            System.out.println("a等于b");
//        }
//
//        if(a.compareTo(b) == 1){
//            System.out.println("a大于b");
//        }
//
//        if(a.compareTo(b) > -1){
//            System.out.println("a大于等于b");
//        }
//
//        if(a.compareTo(b) < 1){
//            System.out.println("a小于等于b");
//        }
    }

}
